# Maintainer: Alexander F Rødseth <xyproto@archlinux.org>
# Contributor: Bartłomiej Piotrowski <barthalion@gmail.com>
# Contributor: Larry Hajali <larryhaja[at]gmail[dot]com>
# Contributor: Hubert Grzeskowiak <arch at nemesis13 dot de>

pkgname=tinyxml
pkgver=2.6.2
pkgrel=10
pkgdesc='Simple, small XML parser'
url='http://grinninglizard.com/tinyxml/'
arch=(loong64 x86_64)
license=(zlib)
makedepends=(setconf)
source=("https://downloads.sourceforge.net/$pkgname/${pkgname}_${pkgver//./_}.tar.gz"
        entity.patch
        $pkgname-2.5.3-stl.patch
        $pkgname.pc)
b2sums=('84d4ad0174eb38583a4488523410105928b9797f09a2ef84b41156580a40cc11d1d09e28e67512494bd02d7a7e0b208aa6c1ce3383bec12de606491214c16a77'
        'e8430b2fea965f351c12a0413c3bba43ce19da7e8a0973fae3c7db4b4a8978cbc8b74beb495dd9141e79e90f9ad012b6b5e738207f8075ad7001cb85b424f5be'
        'e9a3d63283f7946e04dafa58a7eeda782b02d6d6093bae18fdec727b9e6c6722a4c610230cc43ea8142695f3a0aaa1e9bf11a8f0072ad580acb5428655a3d6e8'
        '8d7658c143ccc3ebe226f3dfdf96ec96de94bca4168e77c560ec4bea41c5d78245c4a3cdf1f4126361f9214176bfb362a6c8377404ccf2a4c999c3f0d491930c')

prepare() {
  cd $pkgname

  # Fix entity encoding
  patch -p0 -i "$srcdir/entity.patch"

  # Make TINYXML_USE_STL permanently defined in tinyxml.h
  patch -p1 -i "$srcdir/$pkgname-2.5.3-stl.patch"

  # Fix Makefile
  setconf Makefile TINYXML_USE_STL YES
  setconf Makefile RELEASE_CFLAGS "$CXXFLAGS -fPIC"
}

build() {
  cd $pkgname

  make
  g++ -fPIC $CXXFLAGS $LDFLAGS -shared -o "lib$pkgname.so.0.$pkgver" \
      -Wl,-soname,"lib$pkgname.so.0" $(ls *.o | grep -v xmltest)
}

package() {
  cd $pkgname

  install -d -m 0755 "$pkgdir"/usr/{lib,include}
  install -m 0755 "lib$pkgname.so.0.$pkgver" "$pkgdir"/usr/lib/
  install -m 0644 $pkgname.h tinystr.h "$pkgdir"/usr/include
  install -Dm644 readme.txt "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
  install -Dm644 "$srcdir/$pkgname.pc" "$pkgdir/usr/lib/pkgconfig/$pkgname.pc"

  cd "$pkgdir/usr/lib"
  ln -s "lib${pkgname}.so.0.$pkgver" lib$pkgname.so.0
  ln -s "lib${pkgname}.so.0.$pkgver" lib$pkgname.so
}
